import {Util} from "@lowcode/jpaas-common-lib";
export default {
    props: ['formData'],
    data() {
        return {
            tabKey: 'columnConf',
            settingJson: {},
            tabList:[
                "columnConf",
                "tabTreeConf",
                "searchConf",
                "searchViewConf",
                "toolBarConf",
                "funConf"
            ],
            fieldDataAll:[],
            listConfig: {
                //列头配置
                columnConf: {},
                //导航树配置
                tabTreeConf: {},
                //搜索条件配置
                searchConf: {},
                //功能按钮
                toolBarConf: {},
                //PC端JS函数
                funConf: {},
                //查询视图配置
                searchViewConf: {}
            }
        }
    },
    created() {
        this.init()
    },
    methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"formCodegenBusiness",key);
        },
        init() {
            this.settingJson = JSON.parse(this.formData.settingJson)
            let tmpVars = this.settingJson.tmpVars;
            let mainEntity;
            //数据来源：业务模型
            if (this.formData.source == "businessModel") {
                mainEntity=tmpVars["mainEntity"];
            }
            this.fieldDataAll=this.getAttrList(mainEntity);
            this.listConfig = { ...this.settingJson.listConfig }
        },
        toHump(val,flag) {
            let result='';
            if(val) {
                val = val.toLowerCase();
                const arr = val.split('_')
                result=arr.map((ele,idx)=> {
                    if(!ele){
                        return;
                    }
                    return idx === 0 ? flag?ele.toUpperCase():ele.toLowerCase() : ele[0].toUpperCase() + ele.slice(1)
                }).join('');
            }
            return result;
        },
        getColumnByAttr(attr,code,remark){
            code=code.toUpperCase();
            attr=Util.deepClone(attr);
            attr.fieldName=attr.fieldName+code;
            attr.name=attr.name+code;
            attr.comment=attr.comment+remark;
            attr.control="rx-ref";
            return attr;
        },
        getAttrList(entity){
            let genMode=entity.genMode;
            let boAttrList=entity.boAttrList;
            let attrList=[];
            if(genMode!="db"){
                attrList.push({name:"id",fieldName:"ID_",dataType:"varchar",comment:"主键",control:"rx-commonfield",isPk:1})
            }
            for(let i=0;i<boAttrList.length;i++){
                let attr=boAttrList[i];
                if(genMode!="db"){
                    let isSingle=attr.isSingle;
                    if(attr.control=="rx-address"){
                        let extJson=JSON.parse(attr.extJson);
                        let setting=JSON.parse(extJson.setting);
                        let provinceAttr=this.getColumnByAttr(attr,"_p_code","省Code");
                        provinceAttr.control="rx-address";
                        attrList.push(provinceAttr);
                        attrList.push(this.getColumnByAttr(attr,"_province","省"));
                        if(setting.isCity){
                            attrList.push(this.getColumnByAttr(attr,"_city_code","市Code"));
                            attrList.push(this.getColumnByAttr(attr,"_city","市"));
                        }
                        if(setting.isCounty){
                            attrList.push(this.getColumnByAttr(attr,"_county_code","县(区)Code"));
                            attrList.push(this.getColumnByAttr(attr,"_county","县(区)"));
                        }
                        if(setting.isAddress){
                            attrList.push(this.getColumnByAttr(attr,"_address","详细地址"));
                        }
                    }else if(isSingle==0){
                        attrList.push(attr);
                        attrList.push(this.getColumnByAttr(attr,"_name","(名称)"));
                    }else{
                        attrList.push(attr);
                    }
                }else{
                    attrList.push(attr);
                }
            }
            if(genMode!="db"){
                attrList.push({name:"tenantId",fieldName:"TENANT_ID_",dataType:"varchar",comment:"租户ID",control:"rx-commonfield"})
                attrList.push({name:"createBy",fieldName:"CREATE_BY_",dataType:"varchar",comment:"创建人",control:"rx-commonfield"})
                attrList.push({name:"createTime",fieldName:"CREATE_TIME_",dataType:"varchar",comment:"创建时间",control:"rx-commonfield"})
                attrList.push({name:"updateBy",fieldName:"UPDATE_BY_",dataType:"varchar",comment:"更新人",control:"rx-commonfield"})
                attrList.push({name:"updateTime",fieldName:"UPDATE_TIME_",dataType:"varchar",comment:"更新时间",control:"rx-commonfield"})
                attrList.push({name:"createDepId",fieldName:"CREATE_DEP_ID_",dataType:"varchar",comment:"组ID",control:"rx-commonfield"})
                attrList.push({name:"companyId",fieldName:"COMPANY_ID_",dataType:"varchar",comment:"公司ID",control:"rx-commonfield"})
            }
            return attrList.map(field => {
                let json={label: field.comment, value: this.toHump(field.name,false),...field};
                if(json.dataType=='default'){
                    json.dataType=json.dbFieldType;
                }
                return json;
            });
        }
    },
}
